﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ZetaPrints.ZetaClient.Business.ThreadSubSytem
{
    /// <summary>
    /// The item that holds a thread and information
    /// about that thread
    /// </summary>
    public class ThreadItem
    {
        #region public properties
        /// <summary>
        /// The order id that this thread is doing work for
        /// </summary>
        public Guid OrderId { get; set; }
        /// <summary>
        /// List of things that this thread might be doing
        /// </summary>
        public ThreadActionOption ThreadAction { get; set; }
        /// <summary>
        /// The thread that is running, or not
        /// </summary>
        public Thread ThreadEntry { get; set; }
        /// <summary>
        /// Gets a value indicating the execution status of the current thread.
        /// </summary>
        public bool IsAlive
        {
            get
            {
                return ThreadEntry.IsAlive;
            }
        }
        #endregion


        #region constructors
        /// <summary>
        /// Initializes and starts the thread
        /// </summary>
        /// <param name="orderId">A orderid of all zeros will be a system thread</param>
        /// <param name="threadActionList"></param>
        /// <param name="threadToStart"></param>
        public ThreadItem(Guid orderId, ThreadActionOption threadAction, Thread threadToStart)
        {
            OrderId = orderId;
            ThreadAction = threadAction;
            ThreadEntry = threadToStart;
            ThreadEntry.Start();
            ThreadEntry.Name = orderId.ToString() + threadAction.ToString();
        }
        /// <summary>
        /// Initializes and starts the thread
        /// </summary>
        /// <param name="orderId">A orderid of all zeros will be a system thread</param>
        /// <param name="threadActionList"></param>
        /// <param name="threadToStart"></param>
        public ThreadItem(Guid orderId, ThreadActionOption threadAction, Thread threadToStart, object threadParameter)
        {
            OrderId = orderId;
            ThreadAction = threadAction;
            ThreadEntry = threadToStart;
            ThreadEntry.Start(threadParameter);
            ThreadEntry.Name = orderId.ToString() + threadAction.ToString();
        }
        #endregion


        #region public methods
        /// <summary>
        /// Raises a System.Threading.ThreadAbortException in the thread on which it
        /// is invoked, to begin the process of terminating the thread. Calling this
        ///  method usually terminates the thread.
        /// </summary>
        public void Abort()
        {
            ThreadEntry.Abort();
        }
        #endregion
    }
}
